package com.company.crm.workbench.web.controller;


import com.company.crm.commons.constants.Constants;
import com.company.crm.commons.domain.ReturnObject;
import com.company.crm.commons.util.DateUtils;
import com.company.crm.commons.util.UUIDUtils;
import com.company.crm.settings.domain.DicValue;
import com.company.crm.settings.domain.User;
import com.company.crm.settings.service.DicValueService;
import com.company.crm.settings.service.UserService;
import com.company.crm.workbench.domain.Activity;
import com.company.crm.workbench.domain.Clue;
import com.company.crm.workbench.domain.ClueActivityRelation;
import com.company.crm.workbench.service.ActivityService;
import com.company.crm.workbench.service.ClueActivityRelationService;
import com.company.crm.workbench.service.ClueService;
import org.aspectj.apache.bcel.generic.RET;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 作者：王磊栋
 * 2021/4/22
 */
@Controller
public class ClueController {
    @Autowired
    private ClueService clueService;
    @Autowired
    private UserService userService;
    @Autowired
    private DicValueService dicValueService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ClueActivityRelationService clueActivityRelationService;

    @RequestMapping("/workbench/clue/index.do")
    public String showCluePage(Model model){
        //获取所有的用户
        List<User> users = userService.selectAll();
        model.addAttribute("userList", users);
        //通过根据字典类型字典值获取所有的字典值
        List<DicValue> dicValueList = dicValueService.selectDicValueByCodeType("appellation");
        model.addAttribute("appellationList", dicValueList);
        List<DicValue> sourceList = dicValueService.selectDicValueByCodeType("source");
        model.addAttribute("sourceList",sourceList);
        List<DicValue> clueStateList = dicValueService.selectDicValueByCodeType("clueState");
        model.addAttribute("clueStateList", clueStateList);
        return "workbench/clue/index";
    }

    @RequestMapping("/workbench/clue/saveCreateClue.do")
    @ResponseBody
    public Object saveCreateClue(Clue clue, HttpSession session){
        User user = (User) session.getAttribute(Constants.SESSION_USER);
        clue.setId(UUIDUtils.getUUID());
        clue.setCreateBy(user.getId());
        clue.setCreateTime(DateUtils.formatDateTime(new Date()));
        int result = clueService.insertClueByClue(clue);
        ReturnObject ro = new ReturnObject();
        if (result>0){
            ro.setCode(Constants.LOGIN_CODE_SUCCESS);
        }else {
            ro.setCode(Constants.LOGIN_CODE_FAIL);
            ro.setMessage("增加失败");
        }
        return ro;
    }

    //线索详情
    @RequestMapping("/workbench/clue/detailClue.do")
    public String detailClue(Model model,String id){
        Clue clue = clueService.selectClueForDetailById(id);
        //根据线索拿到市场活动在通过线索的绑定的市场活动id在得到所有的市场活动。
        //ClueActivityRelation clueActivityRelation = clueActivityRelationService.selectByPrimaryKey(id);
        List<ClueActivityRelation> lists = clueActivityRelationService.selectClueActivityRelationByClueId(id);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < lists.size(); i++) {
            list.add(lists.get(i).getActivityId());
        }
        String[] ids = new String[list.size()];
        List<Activity> activityList = activityService.selectActivityByIds(list.toArray(ids));
        model.addAttribute("activityList",activityList);
        model.addAttribute("clue", clue);
        return "workbench/clue/detail";        
    }
    @RequestMapping("/workbench/clue/searchActivity.do")
    @ResponseBody
    public Object searchActivity (String activityName,String clueId){
        List<Activity> activityList = activityService.selectActivityByClueID(activityName, clueId);

        return activityList;
    }


    //通过复选框可以拿到活动的id值
    //当前的线索的clueId也能拿到。
    //那么可以
    @RequestMapping("/workbench/clue/saveBundActivity.do")
    @ResponseBody
    public Object saveBundActivity(String[] activityId,String clueId){
        ReturnObject ro = new ReturnObject();
        List<ClueActivityRelation> list = new ArrayList<>();
        //在多对多的中间表中插入数据
        ClueActivityRelation car = null;
        for (int i = 0; i < activityId.length; i++) {
            car = new ClueActivityRelation();
            car.setId(UUIDUtils.getUUID());
            car.setClueId(clueId);
            car.setActivityId(activityId[i]);
            list.add(car);
        }
        int result = clueActivityRelationService.insertClueActivityRelation(list);
        if (result>0){
            ro.setCode(Constants.LOGIN_CODE_SUCCESS);
            List<Activity> activityList = activityService.selectActivityByIds(activityId);
            ro.setResData(activityList);
        }else {
            ro.setMessage("关联失败");
            ro.setCode(Constants.LOGIN_CODE_FAIL);
        }
        return ro;
    }
    @RequestMapping("/workbench/clue/saveUnbundActivity.do")
    @ResponseBody
    public Object saveUnbundActivity (ClueActivityRelation clueActivityRelation){
        ReturnObject ro = new ReturnObject();
        int result = clueActivityRelationService.unBundActivity(clueActivityRelation);
        if (result>0){
            ro.setCode(Constants.LOGIN_CODE_SUCCESS);
        }else {
            ro.setCode(Constants.LOGIN_CODE_FAIL);
            ro.setMessage("解除失败");
        }
        return ro;
    }

    //装换
    @RequestMapping("/workbench/clue/convertClue.do")
    public String convertClue(Model model,String id){
        Clue clue = clueService.selectClueForDetailById(id);
        model.addAttribute("clue", clue);
        List<DicValue> stageList = dicValueService.selectDicValueByCodeType("stage");
        model.addAttribute("stageList", stageList);
        return "workbench/clue/convert";
    }
    @RequestMapping("/workbench/clue/queryActivityForDetailByName.do")
    @ResponseBody
    public Object queryActivityForDetailByName(String activityName){
        List<Activity> activityList = activityService.selectActivityByClueID(activityName, null);
        return activityList;
    }
    /*********************************************************************/
    //转换
    @RequestMapping("/workbench/clue/saveConvertClue.do")
    @ResponseBody
    public Object saveConvertClue(String clueId,
                                  String isCreateTran,
                                  String amountOfMoney,
                                  String tradeName,
                                  String expectedClosingDate,
                                  String stage,
                                  String activityId,
                                  HttpSession session){

        Map<String,Object> map = new HashMap<>();
        map.put("clueId",clueId);
        map.put("isCreateTran",isCreateTran);
        map.put("amountOfMoney",amountOfMoney);
        map.put("tradeName",tradeName);
        map.put("expectedClosingDate",expectedClosingDate);
        map.put("stage",stage);
        map.put("activityId",activityId);
        map.put(Constants.LOGIN_CODE_SUCCESS,session.getAttribute(Constants.SESSION_USER));
        //调用clueService进行调用
        int result = clueService.ConvertClue(map);
        ReturnObject  ro = new ReturnObject();
        if (result>0){
            ro.setCode(Constants.LOGIN_CODE_SUCCESS);
        }else {
            ro.setCode(Constants.LOGIN_CODE_FAIL);
            ro.setMessage("转化失败");
        }

        return ro;
    }

}
